package cn.cxg.ymcc.web.controller;

import cn.cxg.ymcc.result.JSONResult;
import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * 短信验证码
 */
@RequestMapping("/oss")
@RestController
public class OSSController {

    @PostMapping("/uploadFile")
    public JSONResult uploadFile(@RequestParam MultipartFile fileName){


        // Endpoint以华东1（杭州）为例，填写为https://oss-cn-hangzhou.aliyuncs.com，其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        String bucketName = ("cxg-ymcc");
        // 填写Bucket所在地域。以华东1（杭州）为例，Region填写为cn-hangzhou。
        String region = "cn-hangzhou";

        // 从环境变量中获取访问凭证。运行本代码示例之前，请先配置环境变量
//        EnvironmentVariableCredentialsProvider credentialsProvider =
//                CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();

        // 创建OSSClient实例。
        // 当OSSClient实例不再使用时，调用shutdown方法以释放资源。
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        // 显式声明使用 V4 签名算法
//        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
//        OSS ossClient = OSSClientBuilder.create()
//                .endpoint(endpoint)
//                .credentialsProvider(credentialsProvider)
//                .region(region)
//                .build();

        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = new OSSClientBuilder().build(endpoint,
                "LTAI5tMe5AqQ4uov1e8k5BSK",
                "ADlMgDWvP2KYbPRWsbWuj9J4XguM3R");


        try {
            // 2. 上传文件
            String format = new SimpleDateFormat("yyyy/MM/dd").format(new Date());
            //后缀
            String substring = fileName.getOriginalFilename().substring(fileName.getOriginalFilename().lastIndexOf("."));
            //拼接
            String objectName = format+"/"+System.currentTimeMillis()+substring;
            //上传
            ossClient.putObject(bucketName,objectName,fileName.getInputStream());

//            String objectName = "exampledir/exampleobject.txt";
//            String content = "Hello OSS";
//            ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(content.getBytes()));


            System.out.println("2. 文件 " + objectName + " 上传成功。");

            //https://cxg-ymcc.oss-cn-hangzhou.aliyuncs.com/2025/08/12/1754988614447.txt
            return JSONResult.success("https://cxg-ymcc.oss-cn-hangzhou.aliyuncs.com"+objectName);
        } catch (Exception oe) {
            oe.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

        return JSONResult.error("上传文件失败");
    }





}
